from datetime import datetime

import matplotlib.pyplot as plt



def plot_singleModel(model_name, accuracy_list, precision_list, recall_list, f1_list):
    # 创建一个4行1列的画布
    figure, axes = plt.subplots(nrows=4, ncols=1, sharex=True)

    # 绘图对象
    # 选择ax1
    plt.sca(axes[0])
    plt.title(model_name)
    # axes[0].set_title("Neural Networks")
    # axes[0].set_ylabel("accuracy")
    axes[0].plot(range(1, 11), accuracy_list, 'red', label='Accuracy')
    axes[0].legend(loc='lower right', ncol=1, shadow=True, fancybox=True)

    # axes[1].set_ylabel("precision")
    axes[1].plot(range(1, 11), precision_list, 'blue', label='Precision')
    axes[1].legend(loc='lower right', ncol=1, shadow=True, fancybox=True)

    # axes[2].set_ylabel("recall")
    axes[2].plot(range(1, 11), recall_list, 'green', label='Recall')
    axes[2].legend(loc='lower right', ncol=1, shadow=True, fancybox=True)

    # axes[3].set_ylabel("f1")
    axes[3].plot(range(1, 11), f1_list, 'darkkhaki', label='F1')
    axes[3].legend(loc='lower right', ncol=1, shadow=True, fancybox=True)
    # plt.plot(range(1, 11), precision_list, label='Decision Tree')
    # plt.plot(range(1, 11), recall_list, label='SVM')
    # plt.plot(range(1, 11), f1_list, label='STACKING')

    img_name = model_name + '_' + datetime.now().strftime('%m-%d-%H-%M')
    savePath = 'results/'+img_name+'.png'
    print("结果已保存到%s" % savePath)
    plt.savefig(savePath, dpi=300)
    plt.show()


def plot_multiModel(models, models_name, color_list, accuracy_data, precision_data, recall_data, f1_data):
    # 导出结果图片名称
    img_name = ""
    # 绘图
    figure, axes = plt.subplots(nrows=2, ncols=2)
    # 依次画出所有选中模型对应的评价指标
    for i in range(0, len(models)):
        axes[0][0].set_title("Accuracy")
        axes[0][0].plot(range(1, 11), accuracy_data[i], color_list[models[i]], label=models_name[models[i]])
        axes[0][1].set_title("Precision")
        axes[0][1].plot(range(1, 11), precision_data[i], color_list[models[i]], label=models_name[models[i]])
        axes[1][0].set_title("Recall")
        axes[1][0].plot(range(1, 11), recall_data[i], color_list[models[i]], label=models_name[models[i]])
        axes[1][1].set_title("F1")
        axes[1][1].plot(range(1, 11), f1_data[i], color_list[models[i]], label=models_name[models[i]])
        img_name += models_name[models[i]] + '-'

    axes[1][1].legend(loc='upper center', bbox_to_anchor=(-0.1, -0.1), ncol=len(models), shadow=True, fancybox=True)

    img_name += datetime.now().strftime('%m-%d-%H-%M')
    savePath = 'results/' + img_name + '.png'
    print("结果已保存到%s" % savePath)
    plt.savefig(savePath, dpi=300)
    plt.show()
